#include "UI.h"
#include "AppDelegate.h"

USING_NS_CC;

Color4F UI::themeColorF(GLubyte alpha)
{
	return Color4F(Color4B(theme_r, theme_g, theme_b, alpha));
}

Color4B UI::themeColorB(GLubyte alpha)
{
	return Color4B(theme_r, theme_g, theme_b, alpha);
}

Color3B UI::themeColor3B()
{
	return Color3B(theme_r, theme_g, theme_b);
}

Color4F UI::textColorF(GLubyte alpha)
{
	return Color4F(Color4B(text_r, text_g, text_b, alpha));
}

Color4B UI::textColorB(GLubyte alpha)
{
	return Color4B(Color4F(Color4B(text_r, text_g, text_b, alpha)));
}

Color3B UI::textColor3B()
{
	return Color3B(text_r, text_g, text_b);
}

Color4F UI::uicombgColorF()
{
	return Color4F(Color4B(uicom_bg_r, uicom_bg_g, uicom_bg_b, uicom_bg_a));
}

Color4B UI::uicombgColorB()
{
	return Color4B(uicom_bg_r, uicom_bg_g, uicom_bg_b, uicom_bg_a);
}

Color3B UI::uicombgColor3B()
{
	return Color3B(uicom_bg_r, uicom_bg_g, uicom_bg_b);
}

Color4F UI::uicomfgColorF()
{
	return Color4F(Color4B(uicom_fg_r, uicom_fg_g, uicom_fg_b, uicom_fg_a));
}

Color4B UI::uicomfgColorB()
{
	return Color4B(uicom_fg_r, uicom_fg_g, uicom_fg_b, uicom_fg_a);
}

Color3B UI::uicomfgColor3B()
{
	return Color3B(uicom_fg_r, uicom_fg_g, uicom_fg_b);
}

void UI::setWndSize(float width)
{
	auto newSize = Size(width, width * 9 / 16);
	auto glv = Director::getInstance()->getOpenGLView();
	auto hwnd = glv->getWin32Window();
	int wndleft = (GetSystemMetrics(SM_CXSCREEN) - newSize.width)*.5f;
	int wndtop = (GetSystemMetrics(SM_CYSCREEN) - newSize.height)*.5f;
	MoveWindow(hwnd, wndleft, wndtop, newSize.width, newSize.height, true);
	glv->setFrameZoomFactor(newSize.width / glv->getFrameSize().width);
}

void UI::setWndTItle(const wchar_t* title)
{
	auto hwnd = Director::getInstance()->getOpenGLView()->getWin32Window();
	wstring newtitle = L"- Infinity DreaMus - ";
	newtitle.append(title);
	DefWindowProc(hwnd, WM_SETTEXT, NULL, (LPARAM)newtitle.c_str());
}

Size UI::getNodeScaledSize(Node * node)
{
	Size s = node->getContentSize();
	s.width *= node->getScaleX();
	s.height *= node->getScaleY();
	return s;
}

void UI::setNodeFillInArea(Node * node, Size fillarea)
{
	auto ratio = fillarea.width / fillarea.height;
	auto nodesize = node->getContentSize();
	if (nodesize.width / nodesize.height <= ratio) node->setScale(fillarea.width / nodesize.width);
	else node->setScale(fillarea.height / nodesize.height);
}

void UI::setNodeFitInArea(Node * node, Size fillarea)
{
	auto ratio = fillarea.width / fillarea.height;
	auto nodesize = node->getContentSize();
	if (nodesize.width / nodesize.height <= ratio) node->setScale(fillarea.height / nodesize.height);
	else node->setScale(fillarea.width / nodesize.width);
}

void UI::setNodeAnch(Node * node, Vec2 anch)
{
	node->ignoreAnchorPointForPosition(false);
	node->setAnchorPoint(anch);
}

Vec2 UI::convertOrbitPos(Vec2 worldPos, float areaPosX, float areaWidth, int angelZ)
{
	Vec2 ret;
	if (angelZ == -90)
	{
		float scalex = 1.47f;
		float ax = areaPosX;
		float ay = frameSize.height*.5f;
		float px2ax = worldPos.x - ax;
		float py2ay = worldPos.y - ay;
		float xp = px2ax / areaWidth;
		float scaley = 1 + .77f*xp;
		ret.x = ax + (px2ax * scalex);
		ret.y = ay + (py2ay * scaley);
	}
	else if (angelZ == 90)
	{
		float scalex = .95f;
		float ax = areaPosX;
		float ay = frameSize.height*.5f;
		float px2ax = worldPos.x - ax;
		float py2ay = worldPos.y - ay;
		float xp = px2ax / areaWidth;
		float scaley = 1 - .42f*xp;
		ret.x = ax + (px2ax * scalex);
		ret.x += -150 * pow(xp, 2) + 150 * xp - 10;
		ret.y = ay + (py2ay * scaley);
	}
	else return Vec2::ZERO;

	return ret;
}

void UI::setEventFlagByBit(unsigned short flagbitpos)
{
	pUI->_setEventFlagByBit(flagbitpos);
}

void UI::setEventFlag(unsigned int flag)
{
	pUI->eventFlag = flag;
}

unsigned int UI::moveEventFlagUp()
{
	auto newflag = pUI->eventFlag << 1;
	if (newflag) pUI->eventFlag = newflag;
	return newflag;
}

unsigned int UI::moveEventFlagDown()
{
	auto newflag = pUI->eventFlag >> 1;
	if (newflag) pUI->eventFlag = newflag;
	return newflag;
}

unsigned int UI::getEventFlag()
{
	return pUI->_getEventFlag();
}


void UI::_setEventFlagByBit(unsigned short flagbitpos)
{
	if (flagbitpos <= 16 && flagbitpos > 0)
	{
		eventFlag = 0x0001 << (flagbitpos - 1);
	}
}

unsigned int UI::_getEventFlag()
{
	return eventFlag;
}

UI::UI(Size s)
{
	visibleSize = s;
}

float UI::dsnSize(float p1920)
{
	return p1920*(visibleSize.width / 1920);
}

